package com.gxa.service.impl;

import com.alibaba.excel.EasyExcel;
import com.gxa.common.ConfirmedCaseReadListener;
import com.gxa.common.vo.ConfirmedCase;
import com.gxa.mapper.ImportExcelMapper;
import com.gxa.service.ImportExcelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class ImportExcelServiceImpl implements ImportExcelService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ImportExcelServiceImpl.class);

    @Autowired
    private ImportExcelService importExcelService;

    @Autowired
    private ImportExcelMapper importExcelMapper;

    @Override
    public void dealFile(MultipartFile file) {
        InputStream is = null;
        try {
            is = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //1.进行读取数据，ConfirmedCase是我的pojo类，
        //2.new SorageListener(storageService)这个是监听器，主要用来读取数据的，别急后面会讲
        //3.特别注意的是storageService这个service，我上面有注入进去 @Autowired，切记不要new会报错
        EasyExcel.read(is, ConfirmedCase.class, new ConfirmedCaseReadListener(importExcelService)).sheet().doRead();
    }

    @Override
    public void save(List<ConfirmedCase> list) {
        LOGGER.info("落库开始------");
        importExcelMapper.insertConfirmedCase(list);
    }
}
